package uia.alumni.group;

import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.http.HttpServletRequest;
import uia.alumni.data.group;
import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.useradm.Constants;
import uia.alumni.web.Page;



/**
 * Display a user profile. If the parameter UID contains a valid
 * user id that user's profile is shown. Otherwise the profile
 * of the logged in user is displayed.
 *
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class ViewGroup  extends Page implements Constants {

    /** SERVLET_NAME is used for consistency checking of web.xml */
    public static final String SERVLET_NAME = "profile.view";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;


    public ViewProfile() {
        super(Role.member);
    }


    /**
     * Write a page displaying the profile of the user specified
     * by the paraameter UID.
     * @param out
     * @param request
     */
    public void writeBody(PrintWriter out, HttpServletRequest request) {
        long uid = parseLong(request.getParameter(UID), -1);
        EntityManager em = emf.createEntityManager();
        boolean ownProfile = false;
        User user = em.find(User.class, uid);

        if (user == null) {
            user = getLoggedInUser(em, request);
            ownProfile = true;
        }

        out.println("<h1>"+user.getFullName()+"'s Profile</h1>");

        if (ownProfile) {
            out.print("<p>");
            printLink(out, "[Edit Profile]", EditProfile.SERVLET_NAME);
            out.print(" ");
            printLink(out, "[Edit CV]", EditCv.SERVLET_NAME);
            out.print(" ");
            printLink(out, "[Change Password]", EditPassword.SERVLET_NAME);
            out.print(" ");
            out.println("</p>");
        }

        out.println("<p>"+user.getDescription()+"</p>");

        out.println("<h2>CV</h2>");
        out.println("<table>");
        out.println("<tr><th>When</th><th>What</th></tr>");
        for (CvEntry cve : user.getCv()) {
            out.println("<tr><td>"
                    +cve.getFromDate()+"-<br/>"+cve.getToDate()
                    +"</td><td><b>"+cve.getType()+"</b><br/>"
                    +cve.getDescription()+"</td></tr>");
        }
        out.println("</table>");

        out.println("<h2>Contact</h2>");
        out.println("<table>");
        out.println("<tr><td>Address</td><td>"+user.getAddress()+"</td></tr>");
        out.println("<tr><td>Phone</td><td>"+user.getPhone()+"</td></tr>");
        out.println("<tr><td>Email</td><td>"+user.getUserName()+"</td></tr>");
        out.println("</table>");

        if (ownProfile) {
            out.print("<p>");
            printLink(out, "[Edit Profile]", EditProfile.SERVLET_NAME);
            out.print(" ");
            printLink(out, "[Edit CV]", EditCv.SERVLET_NAME);
            out.print(" ");
            printLink(out, "[Change Password]", EditPassword.SERVLET_NAME);
            out.print(" ");
            out.println("</p>");
        }
}

    public final static long serialVersionUID = 1;

}